﻿using System;
using System.Collections.Generic;
using System.Text;
using Boomerang.Common.Sms.BoomerangService;
using Boomerang.Common.Crm;
using System.Diagnostics;
using System.Web.Services.Protocols;

namespace Boomerang.Common.Sms
{
    public class SmsDataSource
    {
        SmsConnection connection;

        public SmsDataSource(SmsConnection connection)
        {
            this.connection = connection;
        }

        public int SendSms(string mobileNo, string text, string crmId)
        {
            try
            {

                // Modified by Taner
                return connection.Service.SendTextMessage(connection.BoomerangUserName,
                    connection.BoomerangPassword, connection.LicenseKey, string.Empty , crmId,
                    mobileNo, text, string.Empty, string.Empty, false, string.Empty, string.Empty);

            }
            catch (SoapException ex)
            {
                Trace.TraceError("Error sending SMS {0}. {1} \n {2}", crmId, ex.Detail.InnerText, ex.StackTrace);
                throw;
            }
        }

        public Response[] GetAllNewResponses()
        {
            try
            {
                return connection.Service.GetAllNewResponses(connection.BoomerangUserName, connection.BoomerangPassword, connection.LicenseKey, false);
            }
            catch (SoapException ex)
            {
                Trace.TraceError("Error retrieving sms responses for {0}. {1} \n {2}", connection.BoomerangUserName, ex.Detail.InnerText, ex.StackTrace);
                throw;
            }
        }

        public void SetResponseAsRead(int responseId)
        {
            try
            {
                connection.Service.MarkResponseAsReadByResponseId(connection.BoomerangUserName, connection.BoomerangPassword, connection.LicenseKey, responseId);
            }
            catch (SoapException ex)
            {
                Trace.TraceError("Error marking response as read id={0}. {1} \n {2}", responseId, ex.Detail.InnerText, ex.StackTrace);
                throw;
            }
        }

    }
}
